﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Event_Planner
{
    public partial class SignOff_Application : Form
    {
        DB db;
        public SignOff_Application()
        {
            InitializeComponent();

            db = new DB();
        }

        private void btnCheckBalance_Click(object sender, EventArgs e)
        {
            if (this.txtUserId.Text == "")
            {
                MessageBox.Show("Enter the participant's Id");
            }
            else
            {
                int user_Id = Convert.ToInt32(this.txtUserId.Text);
                if (user_Id > 0)
                {
                    double balance = db.getUserBalance(user_Id);
                    this.txtAccountBalance.Text = balance.ToString();
                }
                else
                {
                    MessageBox.Show("Enter a valid participant's Id");
                }
            }
        }

        private void btnSignOff_Click(object sender, EventArgs e)
        {
            if (this.txtUserId.Text == "")
            {
                MessageBox.Show("Enter the participant's Id");
            }
            else
            {
                int user_Id = Convert.ToInt32(this.txtUserId.Text);
                if (user_Id > 0)
                {
                    int accountNumber = db.getUserAccountNumber(user_Id);
                    double balance = db.getUserBalance(user_Id);
                    
                    if (balance > 0)
                    {
                        List<Participant> participantList = getAllParticipants();
                        bool signedOff = false;
                        foreach (Participant p in participantList)
                        {
                            if (p.AttendanceStatus==2 && p.Number==user_Id)
                            {
                                signedOff = true;
                            }
                        }

                        if (signedOff)
                        {
                            MessageBox.Show("The user has signed off already");
                        }
                        else
                        {
                            db.updateParticipantAttendanceStatus(user_Id, 2);
                            MessageBox.Show("You have been successfully signed off");  
                        }
                    }
                    else
                    {
                        MessageBox.Show("You have been successfully signed off");
                    }
                    this.txtUserId.Clear();
                    this.txtAccountBalance.Clear();
                }
                else
                {
                    MessageBox.Show("Enter a valid participant's Id");
                }
            }
        }

        public List<Participant> getAllParticipants()
        {
            List<Participant> participants = new List<Participant>();
            DataTable participantRecords = db.getAllParticipantsFromDB();
            for (int i = 0; i < participantRecords.Rows.Count; i++)
            {
                Participant participant = new Participant();
                participant.Number = Convert.ToInt32(participantRecords.Rows[i]["participant_id"]);
                participant.FirstName = participantRecords.Rows[i]["first_name"].ToString();
                participant.LastName = participantRecords.Rows[i]["last_name"].ToString();
                participant.Email = participantRecords.Rows[i]["email"].ToString();
                participant.Address = participantRecords.Rows[i]["address"].ToString();
                participant.DateOfBirth = participantRecords.Rows[i]["date_of_birth"].ToString();
                participant.RegistrationDate = participantRecords.Rows[i]["registration_date"].ToString();
                participant.PaymentStatus = Convert.ToInt32(participantRecords.Rows[i]["payment_status"]);
                participant.AttendanceStatus = Convert.ToInt32(participantRecords.Rows[i]["attendance_status"]);
                participant.AccountNumber = Convert.ToInt32(participantRecords.Rows[i]["account_number"]);
                participant.Balance = Convert.ToInt32(participantRecords.Rows[i]["balance"]);
                participants.Add(participant);
            }
            return participants;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
